package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AllTheException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.sky.constant.MessageConstant.*;
import static com.sky.constant.StatusConstant.DISABLE;
import static com.sky.constant.StatusConstant.ENABLE;

/**
 * @Author：XueShiHao
 * @Date：2025/6/6 星期五 10:28:23
 * @Email:747278007@qq.com
 * @Description:
 **/
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 保存菜品以及对应的口味
     * 逻辑：保存菜品，根据菜品id再保存相应的口味数据
     * @param dishDTO
     */
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
Dish dish=new Dish();
BeanUtils.copyProperties(dishDTO,dish);
dishMapper.insert(dish);//保存菜品数据
List<DishFlavor> flavors=dishDTO.getFlavors();
if(flavors!=null&&flavors.size()>0){
    flavors.forEach(flavor->{//如果口味集合不为空，遍历设置id，调用dishFlavorMapper.insertBatch()保存口味到菜品口味表
        flavor.setDishId(dish.getId());
        dishFlavorMapper.insertBatch(flavors);
    });
}

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品的删除
     * 1.正在出售的菜品不可以删除
     * 2.关联了相关套餐的菜品不可以删除
     * @param ids
     */

    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id->{
            Dish dish=dishMapper.getById(id);
if(dish.getStatus()==DISABLE){
    throw new AllTheException(SETMEAL_ON_SALE);
}});
        List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null&&setmealIds.size()>0){
            throw new AllTheException(DISH_BE_RELATED_BY_SETMEAL) ;
        }
        ids.forEach(id->{
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        });
    }

    /**
     * 根据id查询菜品
     *
     * 1.先查询菜品的信息
     * 2.根据菜品id查询菜品的口味
     * 3.两者封装再一起，传递给前端
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish=dishMapper.getById(id);
        List<DishFlavor> flavors=dishFlavorMapper.getByDishId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
       return dishVO;
    }

    /**
     * 更新菜品的同时需要更新菜品对应的口味
     * 先删除菜品原先的口味，再保存菜品新的口味
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        dishMapper.update(dish);
        List<DishFlavor> flavors=dishDTO.getFlavors();
       if(flavors!=null&&flavors.size()>0){
           flavors.forEach(dishFlavor -> {
               dishFlavor.setDishId(dishDTO.getId());
               dishFlavorMapper.insertBatch(flavors);
           });
       }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
       Dish dish=Dish.builder()
               .categoryId(categoryId)
                       .status(ENABLE)
               .build();
       return dishMapper.list(dish);
    }

    /**
     *条件查询菜品的同时，给出菜品对应的口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList=new ArrayList<>();
        ArrayList<DishVO> dishVOArrayList=new ArrayList<>();
        dishList.forEach(d->{
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(d,dishVO);
        List<DishFlavor> flavors=dishFlavorMapper.getByDishId(d.getId());
        dishVO.setFlavors(flavors);
        dishVOArrayList.add(dishVO);
        });

        return dishVOArrayList;
    }

    /**
     * 菜品的禁售以及起售
     * 逻辑：菜品禁用的同时，如果菜品关联了相关的套餐，套餐也要禁售
     * @param status
     * @param id
     */

    @Override
    public void startOrStop(Integer status, Long id) {
Dish dish=Dish.builder().
        id(id)
        .status(status)
        . build();
dishMapper.update(dish);
if(status==DISABLE){
    List<Long> dishIds=new ArrayList<>();
    dishIds.add(id);

    List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(dishIds);
    if(setmealIds!=null&&setmealIds.size()>0){
       setmealIds.forEach(setmealId->{
           Setmeal setmeal=Setmeal.builder()
                   .status(status)
                   .id(setmealId)
           .build();
           //TODO
       });
    }
}
    }
}
